{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The model before optimization:\n",
      "\n",
      "graph two-transposes (\n",
      "  %X[FLOAT, 2x3x4]\n",
      ") {\n",
      "  %Y = Transpose[perm = [1, 0, 2]](%X)\n",
      "  %Z = Transpose[perm = [1, 0, 2]](%Y)\n",
      "  return %Z\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "from __future__ import unicode_literals\n",
    "\n",
    "import onnx\n",
    "import os\n",
    "from onnx import optimizer\n",
    "\n",
    "# Preprocessing: load the model contains two transposes.\n",
    "model_path = os.path.join('resources', 'two_transposes.onnx')\n",
    "original_model = onnx.load(model_path)\n",
    "\n",
    "print('The model before optimization:\\n\\n{}'.format(onnx.helper.printable_graph(original_model.graph)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Available optimization passes:\n",
      "\teliminate_deadend\n",
      "\teliminate_identity\n",
      "\teliminate_nop_dropout\n",
      "\teliminate_nop_monotone_argmax\n",
      "\teliminate_nop_pad\n",
      "\teliminate_nop_transpose\n",
      "\teliminate_unused_initializer\n",
      "\textract_constant_to_initializer\n",
      "\tfuse_add_bias_into_conv\n",
      "\tfuse_bn_into_conv\n",
      "\tfuse_consecutive_concats\n",
      "\tfuse_consecutive_log_softmax\n",
      "\tfuse_consecutive_reduce_unsqueeze\n",
      "\tfuse_consecutive_squeezes\n",
      "\tfuse_consecutive_transposes\n",
      "\tfuse_matmul_add_bias_into_gemm\n",
      "\tfuse_pad_into_conv\n",
      "\tfuse_transpose_into_gemm\n",
      "\tlift_lexical_references\n",
      "\tnop\n",
      "\tsplit_init\n",
      "\tsplit_predict\n",
      "\n",
      "The model after optimization:\n",
      "\n",
      "graph two-transposes (\n",
      "  %X[FLOAT, 2x3x4]\n",
      ") {\n",
      "  %Z = Transpose[perm = [0, 1, 2]](%X)\n",
      "  return %Z\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# A full list of supported optimization passes can be found using get_available_passes()\n",
    "all_passes = optimizer.get_available_passes()\n",
    "print(\"Available optimization passes:\")\n",
    "for p in all_passes:\n",
    "    print('\\t{}'.format(p))\n",
    "print()\n",
    "\n",
    "# Pick one pass as example\n",
    "passes = ['fuse_consecutive_transposes']\n",
    "\n",
    "# Apply the optimization on the original serialized model\n",
    "optimized_model = optimizer.optimize(original_model, passes)\n",
    "\n",
    "print('The model after optimization:\\n\\n{}'.format(onnx.helper.printable_graph(optimized_model.graph)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
